﻿
using Business.Base.Model;
using Business.Base.Service;
using Microsoft.Office.Interop.Excel;

using System;
using System.IO;



namespace Business.ScrewPump.Perf
{
    public class ScrewPumpPerfExperimentExcelManager : IExperimentExcelManager
    {
     
       public void setExcel(Experiment exp, Workbook wk)
        {
            Worksheet sheet = wk.Sheets[1];

            ScrewPumpPerfExperiment experiment = (ScrewPumpPerfExperiment)exp;

            #region 展示两页内容
            sheet.Rows[1].Cells[1].Value = experiment.Customer;
            sheet.Rows[1].Cells[10].Value = experiment.Customer;
            #endregion
            
            /*row = sheet.GetRow(2);
            #region 第一行
            //试验编号
            row.GetCell(2).SetCellValue(experiment.ExperimentCode);
            //额定功率
            row.GetCell(6).SetCellValue(experiment.DesignShaftPower / 1000.0);
            //电机额定效率
            row.GetCell(8).SetCellValue(experiment.MotorEfficiency);
            //流量
            row.GetCell(10).SetCellValue(experiment.DesignPressure);*/


            #region 第一行
            //泵型号
            sheet.Rows[3].Cells[3].Value = experiment.PumpModel;
            //额定流量
            sheet.Rows[3].Cells[6].Value = Decimal.Round((Decimal)(experiment.PumpRatedFlow));
            //电机效率
            sheet.Rows[3].Cells[9].Value = Decimal.Round((Decimal)(experiment.MotorRatedEfficiency));

            #endregion

            #region 第二行的设置
            //试验编号
            sheet.Rows[4].Cells[3].Value = experiment.ExperimentCode;
            //额定总压
            sheet.Rows[4].Cells[6].Value = (Double)Decimal.Round((Decimal)experiment.PumpRatedPresure, 2);
            //额定转速
            sheet.Rows[4].Cells[9].Value = (Double)Decimal.Round((Decimal)experiment.ImpellerDia*1000, 2);

            //泵型号
            /*row.GetCell(2).SetCellValue(experiment.PumpModel);
            //额定效率
            row.GetCell(6).SetCellValue(experiment.DesignPumpEfficiency);
            //扬程
            row.GetCell(10).SetCellValue(experiment.ExperimentResultFlow);
            //扬程是否合格
            row.GetCell(11).SetCellValue(experiment.ExperimentResultFlowIsPass ? "是" : "否");*/
            #endregion

            #region 第三行的设置
            //row = sheet.GetRow(4);

            //样品型号
            sheet.Rows[5].Cells[3].Value = experiment.PumpCode;
            sheet.Rows[5].Cells[6].Value = Decimal.Round((Decimal)experiment.PumpRatedEfficiency, 2);
            sheet.Rows[5].Cells[9].Value = Decimal.Round((Decimal)experiment.PumpRatedPower, 2);


            #endregion

            #region 第四行的设置
            //row = sheet.GetRow(5);


            //额定扬程
            sheet.Rows[6].Cells[3].Value = Decimal.Round((Decimal)experiment.PumpRatedPresure, 2);

            //电机额定效率
            sheet.Rows[6].Cells[7].Value = Decimal.Round((Decimal)experiment.MotorRatedEfficiency, 2);

            //泵效率
            //sheet.Rows[6].Cells[11].Value = Decimal.Round((Decimal)experiment.MotorRatedEfficiency, 2);

            //sheet.Rows[6].Cells[11].Value = experiment.ExperimentLevel;
            #endregion

            #region 第七行的设置
            /*row = sheet.GetRow(8);

            //测试人
            row.GetCell(2).SetCellValue(experiment.ExperimentOwner);
            //交点流量
            row.GetCell(6).SetCellValue(experiment.CrossPressure.ToString("F2"));
            //交点扬程
            row.GetCell(10).SetCellValue(experiment.CrossFlow.ToString("F2"));*/


            #endregion

            #region 填写第二页的规定值和实测值

            //额定流量
            sheet.Rows[4].Cells[11].Value = experiment.PumpRatedFlow;
            sheet.Rows[4].Cells[12].Value = experiment.PumpRatedPresure;
            sheet.Rows[4].Cells[13].Value = experiment.PumpRatedPower;
            sheet.Rows[4].Cells[14].Value = experiment.PumpRatedEfficiency;

            sheet.Rows[5].Cells[11].Value = Decimal.Round((Decimal)experiment.MeasureFlow, 2);
            sheet.Rows[5].Cells[12].Value = Decimal.Round((Decimal)experiment.MeasurePresure, 2);
            sheet.Rows[5].Cells[13].Value = Decimal.Round((Decimal)experiment.MeasurePower, 2);
            sheet.Rows[5].Cells[14].Value = Decimal.Round((Decimal)experiment.MeasureEff, 2);

            sheet.Rows[5].Cells[15].Value = (int)experiment.ExperimentLevel+"B";
            sheet.Rows[5].Cells[16].Value = experiment.MeasureEffIsPass && experiment.MeasureFlowIsPass && experiment.MeasurePowerIsPass ? "合格" : "不合格";
            #endregion

            #region 填写记录

            int rowIndex = 8;

            foreach (ScrewPumpPerfExperimentRecord record in experiment.RecordList)
            {
                sheet.Rows[rowIndex + 1].Cells[1].Value = (rowIndex - 8) + 1;
                sheet.Rows[rowIndex + 1].Cells[2].Value = Decimal.Round((Decimal)record.WorkingFlow, 2);
                sheet.Rows[rowIndex + 1].Cells[3].Value = Decimal.Round((Decimal)record.PresureIn, 2);
                sheet.Rows[rowIndex + 1].Cells[4].Value = Decimal.Round((Decimal)record.PresureOut, 2);
                sheet.Rows[rowIndex + 1].Cells[5].Value = Decimal.Round((Decimal)record.Voltage, 2);
                sheet.Rows[rowIndex + 1].Cells[6].Value = Decimal.Round((Decimal)record.ElectricCurrent, 2);
                sheet.Rows[rowIndex + 1].Cells[7].Value = Decimal.Round((Decimal)(record.WorkingPower / 1000.0));
                sheet.Rows[rowIndex + 1].Cells[8].Value = Decimal.Round((Decimal)(record.WorkingSpeed), 2);
                sheet.Rows[rowIndex + 1].Cells[9].Value = Decimal.Round((Decimal)(record.Temperature), 2);

                /*cellStyle.SetFont(font);
                cellStyle.VerticalAlignment = VerticalAlignment.Center;

                row = sheet.CreateRow(rowIndex);

                row.HeightInPoints = 20;

                cell = row.CreateCell(0);
                cell.SetCellValue(rowIndex - 12);
                cell.CellStyle = cellStyle;

                cell = row.CreateCell(1);
                cell.SetCellValue(record.WorkFlow.ToString("F2"));
                cell.CellStyle = cellStyle;

                cell = row.CreateCell(2);
                cell.SetCellValue(record.PresureIn.ToString("F2"));
                cell.CellStyle = cellStyle;

                cell = row.CreateCell(3);
                cell.SetCellValue(record.PresureOut.ToString("F2"));
                cell.CellStyle = cellStyle;

                cell = row.CreateCell(4);
                cell.SetCellValue(record.ElectricCurrent.ToString("F2"));
                cell.CellStyle = cellStyle;

                cell = row.CreateCell(5);
                cell.SetCellValue(record.Voltage.ToString("F2"));
                cell.CellStyle = cellStyle;

                cell = row.CreateCell(6);
                cell.SetCellValue((record.WorkingPower / 1000.0).ToString("F2"));
                cell.CellStyle = cellStyle;

                cell = row.CreateCell(7);
                cell.SetCellValue(record.PresureTotal.ToString("F2"));
                cell.CellStyle = cellStyle;

                cell = row.CreateCell(8);
                cell.SetCellValue(record.WorkingSpeed.ToString("F2"));
                cell.CellStyle = cellStyle;

                cell = row.CreateCell(9);
                cell.SetCellValue(record.StpFlow.ToString("F2"));
                cell.CellStyle = cellStyle;

                cell = row.CreateCell(10);
                cell.SetCellValue(record.StpPresure.ToString("F2"));
                cell.CellStyle = cellStyle;

                cell = row.CreateCell(11);
                cell.SetCellValue((record.StpShaftPower / 1000).ToString("F2"));
                cell.CellStyle = cellStyle;


                cell = row.CreateCell(12);
                cell.SetCellValue(record.StpPumpEfficiency.ToString("F2"));
                cell.CellStyle = cellStyle;

                cell = row.CreateCell(13);
                cell.SetCellValue(record.StpGroupEfficiency.ToString("F2"));
                cell.CellStyle = cellStyle;*/
                rowIndex++;
            }

            #endregion

            sheet.Rows[24].Cells[8].Value = experiment.MotorRatedSpeed;

            //cellStyle = wk.CreateCellStyle();
            //dataFormat = wk.CreateDataFormat();
            //font = wk.CreateFont();

            rowIndex = 26;
            foreach (ScrewPumpPerfExperimentRecord record in experiment.RecordList)
            {
                sheet.Cells[rowIndex + 1, 1].Value = (rowIndex - 26) + 1;
                sheet.Cells[rowIndex + 1, 2].Value = Decimal.Round((Decimal)record.PresureTotal, 2);
                sheet.Cells[rowIndex + 1, 3].Value = Decimal.Round((Decimal)record.WorkingFlow, 2);
                sheet.Cells[rowIndex + 1, 4].Value = Decimal.Round((Decimal)record.StpFlow, 2);
                sheet.Cells[rowIndex + 1, 5].Value = Decimal.Round((Decimal)(record.StpShaftPower), 2);
                sheet.Cells[rowIndex + 1, 6].Value = Decimal.Round((Decimal)record.StpGroupEfficiency, 2);
                sheet.Cells[rowIndex + 1, 7].Value = Decimal.Round((Decimal)record.StpPumpEfficiency, 2);

            /*font.FontName = "宋体";
            font.FontHeightInPoints = 9;
            //设置为文本格式，也可以为 text，即 dataFormat.GetFormat("text");
            cellStyle.DataFormat = dataFormat.GetFormat("@");
            cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;    //下边框线
            cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;      //左边框线
            cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;     //右边框线
            cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.None;       //上边框线
            cellStyle.Alignment = HorizontalAlignment.Center;

            cellStyle.SetFont(font);
            cellStyle.VerticalAlignment = VerticalAlignment.Center;*/
            rowIndex++;
            /*row = sheet.CreateRow(rowIndex);
            row.HeightInPoints = 500;
            for (int i = 0; i < 14; i++)
            {
                cell = row.CreateCell(i);
                cell.CellStyle = cellStyle;
            }
            //row.RowStyle = cellStyle;
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 13));




            IDrawing drawing = sheet.CreateDrawingPatriarch();
            byte[] buff = File.ReadAllBytes(experiment.ChartPath);
            int pic = wk.AddPicture(buff, PictureType.PNG);
            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 10, 0, 10, 0, rowIndex, 13, rowIndex + 1);
            //anchor.AnchorType = 2;
            drawing.CreatePicture(anchor, pic);
            #endregion

            String saveFile = experiment.FilePath;
            try
            {
                FileStream fileStream = File.OpenWrite(saveFile);
                wk.Write(fileStream);
                fileStream.Close();
            }
            catch
            {

                return;
                //Debug.WriteLine(e.Message);*/
            }
            sheet.Rows[45].Cells[3].Value = experiment.ExperimentOwner;
            sheet.Rows[45].Cells[8].Value = experiment.CreateTime;
            sheet.Rows[45].Cells[11].Value = experiment.ExperimentOwner;
            sheet.Rows[45].Cells[15].Value = experiment.CreateTime;
            return;
        }
    }
}
